﻿using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System;

namespace Basic
{
    /// <summary>
    /// 实现简单的键值对存档
    /// </summary>
    public class KVStorage
    {
        /// <summary>
        /// 运行时数据
        /// </summary>
        private Dictionary<string, string> m_dictData;

        /// <summary>
        /// 存档路径
        /// </summary>
        private string m_FilePath;

        /// <summary>
        /// 是否美化存档
        /// </summary>
        private bool m_Beauty = false;

        /// <summary>
        /// 创建本地存储实例
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="beauty"></param>
        public KVStorage(string filepath, bool beauty)
        {
            m_FilePath = filepath;
            m_Beauty = beauty;
            m_dictData = JSON.FromJsonFile<Dictionary<string, string>>(filepath);
            if (m_dictData == null)
            {
                m_dictData = new Dictionary<string, string>(8);
            }
        }
        public KVStorage(string content)
        {
            m_dictData = JSON.FromJson<Dictionary<string, string>>(content);
            if (m_dictData == null)
            {
                m_dictData = new Dictionary<string, string>(8);
            }
        }
        private KVStorage(string filepath, bool beauty, Dictionary<string, string> datas)
        {
            m_FilePath = filepath;
            m_Beauty = beauty;
            m_dictData = datas;
        }

        /// <summary>
        /// 保存数据到可读写目录下
        /// </summary>
        private void SaveData()
        {
            if (string.IsNullOrEmpty(m_FilePath))
            {
                return;
            }

            var content = JSON.ToJson(m_dictData);
            if (m_Beauty)
            {
                Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
                content = reg.Replace(content, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
            }

            FileHelper.WriteFile(m_FilePath, content);
        }

        /// <summary>
        /// 手动保存
        /// </summary>
        public void SaveManual()
        {
            SaveData();
        }

        /// <summary>
        /// 内存存储的数据的条目
        /// </summary>
        /// <returns></returns>
        public int DataCount()
        {
            return m_dictData.Count;
        }

        /// <summary>
        /// 指定键是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyExist(string key)
        {
            return m_dictData.ContainsKey(key);
        }

        /// <summary>
        /// 获取记录
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool GetData(string key, string defaultValue, out string value)
        {
            if (m_dictData.TryGetValue(key, out value))
            {
                return true;
            }
            value = defaultValue;
            return false;
        }

        /// <summary>
        /// 记录
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="saveImmediately">立即保存到文件中</param>
        public void SetData(string key, string value, bool saveImmediately)
        {
            m_dictData[key] = value;

            if (saveImmediately)
            {
                SaveData();
            }
        }

        /// <summary>
        /// 移除记录
        /// </summary>
        /// <param name="key"></param>
        /// <param name="saveImmediately">立即保存到文件中</param>
        public void RemoveData(string key, bool saveImmediately)
        {
            m_dictData.Remove(key);

            if (saveImmediately)
            {
                SaveData();
            }
        }

        /// <summary>
        /// 获取bool值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValueOnNotRecord"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool GetBool(string key, bool defaultValueOnNotRecord, out bool value)
        {
            string s;
            bool isExist = GetData(key, defaultValueOnNotRecord.ToString(), out s);
            value = bool.Parse(s);
            return isExist;
        }

        /// <summary>
        /// 记录bool值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="saveImmediately">立即保存到文件中</param>
        public void SetBool(string key, bool value, bool saveImmediately)
        {
            SetData(key, value.ToString(), saveImmediately);
        }

        /// <summary>
        /// 获取键的字符串值, 若不存在, 则指定的返回默认值
        /// 外界不关注键是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValueOnNotRecord"></param>
        /// <returns></returns>
        public string GetString(string key, string defaultValueOnNotRecord)
        {
            string result;
            if (GetData(key, defaultValueOnNotRecord, out result))
            {
                return result;
            }
            return defaultValueOnNotRecord;
        }

        /// <summary>
        /// 保存数据到指定路径
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="beauty"></param>
        /// <param name="datas"></param>
        public static void SaveDatas(string filepath, bool beauty, Dictionary<string, string> datas)
        {
            var storage = new KVStorage(filepath, beauty, datas);
            storage.SaveData();
        }

        public static Dictionary<string, string> GetDatas(string filepath)
        {
            var storage = new KVStorage(filepath, false);
            return storage.m_dictData;
        }
    }
}
